package com.task.web.controller.core;

import javax.annotation.Resource;

import com.task.core.domain.AdvertiserInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.task.common.annotation.DataScope;
import com.task.common.annotation.Log;
import com.task.common.core.controller.BaseController;
import com.task.common.core.domain.AjaxResult;
import com.task.common.core.page.TableDataInfo;
import com.task.common.enums.BusinessType;
import com.task.common.utils.SecurityUtils;
import com.task.core.dto.request.AdvertiserRequestDTO;
import com.task.core.service.AdvertiserService;
import com.task.core.service.CheckService;

import io.swagger.annotations.Api;

/**
 * @author 迪迦.
 * @date 2024/8/14 15:09
 */
@Api("广告主管理")
@RestController
@RequestMapping("/advertiser")
public class AdvertiserController extends BaseController {

  @Resource
  private AdvertiserService advertiserService;

  @Resource
  private CheckService checkService;


  @GetMapping("/getAdvertiserByPage")
  @PreAuthorize("@ss.hasPermi('advertiser:list')")
  public TableDataInfo getAdvertiserByPage(AdvertiserRequestDTO request) {
    startPage(request.getPageNum(), request.getPageSize());
    return getDataTable(advertiserService.getAdvertiserByPage(request));
  }


  @PreAuthorize("@ss.hasPermi('advertiser:option')")
  @GetMapping("/getAdvertiserForOption")
  public TableDataInfo getAdvertiserForOption(AdvertiserRequestDTO request) {
    return getDataTable(advertiserService.getAdvertiserForOption(request));
  }


  @GetMapping("/getAdvertiserByCode/{advertiserCode}")
  @PreAuthorize("@ss.hasPermi('system:advertiser:edit')")
  public AjaxResult getAdvertiserDetail(@PathVariable String advertiserCode) {
    AdvertiserInfo advertiserInfo = advertiserService.getAdvertiserDetail(advertiserCode);
    return success(advertiserInfo);
  }


  @Log(title = "广告主管理", businessType = BusinessType.INSERT)
  @PostMapping("/addAdvertiser")
  public AjaxResult addAdvertiser(@RequestBody AdvertiserRequestDTO request) {
    advertiserService.addAdvertiser(request);
    return success();
  }

  @Log(title = "广告主管理", businessType = BusinessType.UPDATE)
  @PutMapping("/modifyAdvertiser")
  public AjaxResult modifyAdvertiser(@RequestBody AdvertiserRequestDTO request) {
    checkService.check("data_advertiser", request.getCode());
    advertiserService.modifyAdvertiser(request);
    return success();
  }

  @Log(title = "广告主管理", businessType = BusinessType.DELETE)
  @DeleteMapping("/deleteAdvertiser/{advertiserCode}")
  public AjaxResult deleteAdvertiser(@PathVariable String advertiserCode) {
    checkService.check("data_advertiser", advertiserCode);
    advertiserService.deleteAdvertiser(advertiserCode, SecurityUtils.getLoginUser().getUserId());
    return success();
  }

}
